package com.spring.cloud.study.client.consumer.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhanglianxu
 */
@Slf4j
@RestController
public class ClientController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${service.host.name}")
    private String hostName;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder.build();
    }

    @RequestMapping(value = "/client/query/{queryId}", method = RequestMethod.GET)
    public String test(@PathVariable int queryId) {
        return restTemplate.getForObject(hostName + "/service/query/" + queryId, String.class);
    }
}
